package cc.rengu.igas.route.common.dao.impl;

import cc.rengu.igas.route.common.dao.RuleCfgMapper;
import cc.rengu.igas.route.common.entity.RuleCfg;
import cc.rengu.igas.route.common.enums.SmartRouteRspEnums;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 路由规则配置表操作类型
 *
 * @author xlz
 * @version 1.0
 * @date 2020-3-13
 */
public class RuleCfgMapperImpl implements RuleCfgMapper {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.ROUTE_POOL_NAME);

    @Override
    public RuleCfg getTxnRuleCfg(String instId, String srcChannelId, String transChannelId, String txnGroup) throws Exception {
        int iReturnCode;
        if (StringUtil.isNullorEmpty(instId) || StringUtil.isNullorEmpty(srcChannelId) ||
                StringUtil.isNullorEmpty(transChannelId) || StringUtil.isNullorEmpty(txnGroup)) {
            rglog.error("必须输入机构号<{}>，发起系统<{}>、渠道号<{}>，交易分组<{}>", instId, srcChannelId, transChannelId, txnGroup);
            throw new BizException(SmartRouteRspEnums.MSG_VALID_CHECK_ERROR.getRespCode(), SmartRouteRspEnums.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        RuleCfg rulecfg = new RuleCfg();
        rulecfg.setInstId(instId);
        rulecfg.setSrcChannelId(srcChannelId);
        rulecfg.setTransChannelId(transChannelId);
        rulecfg.setTxnGroup(txnGroup);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);

        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "getTxnRouteInfo");
        if (StringUtil.isNullorEmpty(sqlStr)) {
            rglog.error("获取数据操作SQL失败");
            throw new BizException(SmartRouteRspEnums.TRANS_FAIL.getRespCode(), SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
        }

        iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, rulecfg, rulecfg);
        if (Database.DBS_SUCCESS != iReturnCode &&
                Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("获取智能路由规则配置表失败,iReturnCode:<{}>", iReturnCode);
            throw new BizException(SmartRouteRspEnums.TRANS_FAIL.getRespCode(), SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
        } else if (Database.DBS_NOT_FOUND == iReturnCode) {
            rglog.error("无数据");
            return null;
        }

        return rulecfg;
    }

    @Override
    public List<RuleCfg> getAllRuleCfg() throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);

        String sqlStr = (String) UnifiedCache.get(
                AppParamConstant.SQL_MAPPER_CACHE, "getAllTxnRouteInfo");
        if (StringUtil.isNullorEmpty(sqlStr)) {
            rglog.error("获取数据操作SQL失败");
            throw new BizException(SmartRouteRspEnums.TRANS_FAIL.getRespCode(), SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
        }

        RuleCfg ruleCfg = new RuleCfg();

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, ruleCfg, RuleCfg.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询所有多路比例路由信息失败!");
            return null;
        }
        return objectList.stream().map(item -> (RuleCfg) item).collect(Collectors.toList());
    }
}
